<?php

namespace common\models\c2\entity;

use kzo\base\components\events\LinkManyBehavior;
use kzo\base\models\statics\EntityModelStatus;
use kzo\user\models\BeUser;
use Yii;
use yii\behaviors\BlameableBehavior;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%product}}".
 *
 * @property string $id
 * @property integer $type
 * @property string $sku
 * @property string $name
 * @property string $label
 * @property string $factory_price
 * @property string $supplier_id
 * @property string $currency_id
 * @property string $measure_id
 * @property string $description
 * @property integer $sold_count
 * @property string $created_by
 * @property string $updated_by
 * @property integer $status
 * @property integer $position
 * @property string $created_at
 * @property string $updated_at
 */
class Product extends \kzo\base\models\ActiveRecord
{

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%product}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['type', 'supplier_id', 'currency_id', 'measure_id', 'sold_count', 'created_by', 'updated_by', 'status', 'position'], 'integer'],
            [['name', 'attributeset_ids', 'sku'], 'required'],
            [['factory_price'], 'number'],
            [['description'], 'string'],
            [['sku', 'name', ], 'unique'],
            [['created_at', 'updated_at', 'attributeset_ids'], 'safe'],
            [['sku', 'name', 'label'], 'string', 'max' => 255],
        ];
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            BlameableBehavior::className(),
        ]); // TODO: Change the autogenerated stub
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app.c2', 'ID'),
            'type' => Yii::t('app.c2', 'Type'),
            'sku' => Yii::t('app.c2', 'Sku'),
            'name' => Yii::t('app.c2', 'Name'),
            'label' => Yii::t('app.c2', 'Label'),
            'factory_price' => Yii::t('app.c2', 'Factory Price'),
            'supplier_id' => Yii::t('app.c2', 'Supplier ID'),
            'currency_id' => Yii::t('app.c2', 'Currency ID'),
            'measure_id' => Yii::t('app.c2', 'Measure ID'),
            'description' => Yii::t('app.c2', 'Description'),
            'sold_count' => Yii::t('app.c2', 'Sold Count'),
            'created_by' => Yii::t('app.c2', 'Created By'),
            'updated_by' => Yii::t('app.c2', 'Updated By'),
            'status' => Yii::t('app.c2', 'Status'),
            'position' => Yii::t('app.c2', 'Position'),
            'created_at' => Yii::t('app.c2', 'Created At'),
            'updated_at' => Yii::t('app.c2', 'Updated At'),
        ];
    }

    /**
     * @inheritdoc
     * @return \common\models\c2\query\ProductQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new \common\models\c2\query\ProductQuery(get_called_class());
    }

    /**
     * setup default values
     **/
    public function loadDefaultValues($skipIfSet = true)
    {
        parent::loadDefaultValues($skipIfSet);
    }

    public function getCreator()
    {
        return $this->hasOne(BeUser::className(), ['id' => 'created_by']);
    }

    public function getUpdater()
    {
        return $this->hasOne(BeUser::className(), ['id' => 'updated_by']);
    }

    public function getPackages()
    {
        return $this->hasMany(ProductPackage::className(), ['product_id' => 'id']);
    }

    public function getStock()
    {
        return $this->hasOne(ProductStock::className(), ['product_id' => 'id']);
    }

    public function getProductSkus() {
        return $this->hasMany(ProductSku::className(), ['product_id' => 'id']);
    }

    /**
     * @param string $key
     * @param string $val
     * @param array $params
     * @return array dropdown list options
     */
    public function getProductSkuOptionsList($key = 'id', $val = 'label', $params = []) {
        $options = [];
        if (isset($params['withPrice']) && $params['withPrice']) {
            foreach ($this->productSkus as $model) {
                $options[$model->id] = $model->name . " (" . $model->factory_price . Yii::t('app.c2', 'Yuan') . ")";
            }
        } else {
            $options = ArrayHelper::map($this->getProductSkus()->all(), $key, $val);
        }
        return $options;
    }

    public function getProductPackageOptionsList($key = 'id', $val = 'label', $params = []) {
        return ArrayHelper::map($this->getPackages()->all(), $key, $val);
    }

}
